#!/bin/bash

source util.sh

#
# export primaryhost=192.168.0.11
# export primaryname=cm-docker-0-0002
# export standbyhost=192.168.0.94,192.168.0.192
# export standbyname=cm-docker-0-0001,cm-docker
#
#

function set_envfile()
{
    export GPHOME=${tool_path}
    export PATH=$GPHOME/script/gspylib/pssh/bin:$GPHOME/script:$PATH
    export LD_LIBRARY_PATH=$GPHOME/lib:$LD_LIBRARY_PATH
    export PYTHONPATH=$GPHOME/lib
    export PGHOST=${tmp_path}
    export GAUSSHOME=${app_path}
    export LD_LIBRARY_PATH=$GAUSSHOME/lib:$LD_LIBRARY_PATH
    export PATH=${GAUSSHOME}/bin:${PATH}
    export GAUSSLOG=${log_path}

    echo "export GPHOME=${tool_path}" > ${ENVFILE}
    echo "export PATH=$GPHOME/script/gspylib/pssh/bin:$GPHOME/script:$PATH" >> ${ENVFILE}
    echo "export LD_LIBRARY_PATH=$GPHOME/lib:$LD_LIBRARY_PATH" >> ${ENVFILE}
    echo "export PYTHONPATH=$GPHOME/lib" >> ${ENVFILE}
    echo "export PGHOST=${tmp_path}" >> ${ENVFILE}
    echo "export GAUSSHOME=${app_path}" >> ${ENVFILE}
    echo "export LD_LIBRARY_PATH=$GAUSSHOME/lib:$LD_LIBRARY_PATH" >> ${ENVFILE}
    echo "export PATH=${GAUSSHOME}/bin:${PATH}" >> ${ENVFILE}
    echo "export GAUSSLOG=${log_path}" >> ${ENVFILE}
    echo "export GAUSS_ENV=2" >> ${ENVFILE}

    echo "# dir env" >> ${ENVFILE}
    echo "export datanode_dir=${datanode_dir}" >> ${ENVFILE}
    echo "export cm_config_path=${cm_config_path}" >> ${ENVFILE}
    echo "export primaryhost=${primary_host}" >> ${ENVFILE}
    echo "export standbyhost=${standby_hosts}" >> ${ENVFILE}
    echo "export primaryname=${primary_name}" >> ${ENVFILE}
    echo "export standbyname=${standby_names}" >> ${ENVFILE}


    chown $USER:$GROUP ${ENVFILE}
}

function install_app()
{
    cd ${package_path}
    tar -xf ${enterprise_pkg_file} -C .
    plat_info=$(ls openGauss*.tar.bz2 |sed 's/openGauss-\(.*\)-64bit.tar.bz2/\1/g')
    tar -xf openGauss-${plat_info}-64bit.tar.bz2 -C ${app_path}
    tar -xf openGauss-${plat_info}-64bit-cm.tar.gz -C ${app_path}
    tar -xf openGauss-${plat_info}-64bit-om.tar.gz -C ${tool_path}
    # copy create trust script
    cp $ROOT_DIR/gs_createtrust.py ${tool_path}/script
}

function create_dirs()
{
    if [ -d ${install_path} ]; then
        echo "clear old path..."
        rm -rf ${install_path}
    fi

    dir_array=($app_path $log_path $tmp_path $tool_path $datanode_dir $package_path $cm_agent_path $cm_server_path $cm_agent_log $cm_server_log $monitor_log)
    # dir_array=($app_path $log_path)
    for i in ${dir_array[*]}
    do
    echo "${i}"
    mkdir -p "${i}"
    done
}

function create_to_user()
{
    userinfo=$(cat /etc/passwd | grep -w $USER)
    if [ "${userinfo}" != "" ]; then
        echo "${DEFAULT_PASSWD}" | passwd $USER --stdin
        return 0
    fi
    groupadd $GROUP
    useradd -g $GROUP -m $USER
    echo "${DEFAULT_PASSWD}" | passwd $USER --stdin

    # config kown hosts to null
    # ssh-keyscan -t ed25519 hostip has no response in docker
    mkdir -m 700 -p /home/omm/.ssh
    touch /home/omm/.ssh/config
    echo "StrictHostKeyChecking no" > /home/omm/.ssh/config
    echo "UserKnownHostsFile /dev/null" >> /home/omm/.ssh/config
    echo "LogLevel ERROR" >> /home/omm/.ssh/config
    chmod 600 /home/omm/.ssh/config
    chown $USER:$GROUP /home/omm/.ssh
    chown $USER:$GROUP /home/omm/.ssh/config
}


function config_sshd()
{
    # 在容器里面不加该配置，会导致ssh连接拒绝
    # ssh: connect to host 172.18.0.2 port 22: Connection refused
    ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
    ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
    ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
    ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key -N ''
    mkdir -p /var/run/sshd

    /usr/sbin/sshd -D &
}

function write_local_host()
{
    hostfile=/etc/hosts
    hostarr=(${primary_host})
    standby_hostarr=(${standby_hosts})
    hostarr+=(${standby_hostarr//,/ })

    hostnamearr=(${primaryname})
    standby_hostnamearr=(${standby_names})
    hostnamearr+=(${standby_names//,/ })

    index=1
    for(( i=0;i<${#hostarr[@]};i++)) 
    do
        echo "${hostarr[i]} ${hostnamearr[i]} #GAUSS OM" >> /etc/hosts
        echo "${hostarr[i]}" >> /home/omm/hostfile
    done;

    chown $USER:$GROUP /home/omm/hostfile
}


function start_monitor_dead_loop()
{
    i=1
    while ((i > 0))
    do 
        # monitor_proc=$(ps -ef | grep om_monitor | grep -v grep)
        # if [ "${monitor_proc}" == "" ]; then
        #     su - $USER -c "source ${ENVFILE};nohup $GAUSSHOME/bin/om_monitor -L $GAUSSLOG/cm/om_monitor &"
        # fi
        sleep 100;
    done
}

function check_env_hosts()
{
    primary_host=$(echo ${primaryhost} | sed 's/ //g')
    if [ "${primary_host}" == "" ]; then
        echo "not found primary host"
        exit 1
    fi
    standby_hosts=$(echo ${standbyhost} | sed 's/ //g')
    if [ "${standby_hosts}" == "" ]; then
        echo "not found primary host"
        exit 1
    fi
    primary_name=$(echo ${primaryname} | sed 's/ //g')
    if [ "${primary_name}" == "" ]; then
        echo "not found primary host name"
        exit 1
    fi

    standby_names=$(echo ${standbyname} | sed 's/ //g')
    if [ "${standby_names}" == "" ]; then
        echo "not found standby host name"
        exit 1
    fi
}
function generate_xml()
{
    echo "python3 /usr/local/bin/generatexml.py --primary-host=${primary_host} --standby-host=${standby_hosts} --primary-hostname=${primary_name} --standby-hostname=${standby_names}"
    python3 /usr/local/bin/generatexml.py --primary-host=${primary_host} --standby-host=${standby_hosts} --primary-hostname=${primary_name} --standby-hostname=${standby_names}
    if [ -f "/home/omm/cluster.xml" ]; then
        chown omm:omm /home/omm/cluster.xml
    fi
}

function main()
{
    cd /ROOT_DIR
    # if [ ! -f ${all_package_file} ]; then
    #     echo "Not found ${all_package_file} package file."
    #     exit 1
    # fi


    check_env_hosts
    create_to_user
    write_local_host
    config_sshd
    create_dirs
    
    set_envfile
    install_app
    generate_xml

    chown -R $USER:$GROUP ${install_path}

    su - $USER -c 'sh install.sh'

    # start_monitor_dead_loop
    su - $USER -c "source ${ENVFILE};nohup $GAUSSHOME/bin/om_monitor -L $GAUSSLOG/cm/om_monitor &"
    start_monitor_dead_loop
}


main $@